﻿package mortal.game.manager
{
    import __AS3__.vec.*;
    import com.gengine.global.*;
    import flash.events.*;
    import flash.geom.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.mvc.core.*;

    public class StageMouseManager extends Object
    {
        protected var recObjList:Vector.<RecObj>;
        private static var _instance:StageMouseManager;

        public function StageMouseManager()
        {
            this.recObjList = new Vector.<RecObj>;
            return;
        }// end function

        public function start() : void
        {
            Global.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMove);
            Global.stage.addEventListener(MouseEvent.CLICK, this.onMouseClick);
            return;
        }// end function

        public function stop() : void
        {
            Global.stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMove);
            Global.stage.removeEventListener(MouseEvent.CLICK, this.onMouseClick);
            return;
        }// end function

        protected function onMouseMove(event:MouseEvent) : void
        {
            var _loc_4:Boolean = false;
            Dispatcher.dispatchEvent(new DataEvent(EventName.StageMouseMove, event));
            var _loc_2:* = this.recObjList.length;
            var _loc_3:* = _loc_2 - 1;
            while (_loc_3 >= 0)
            {
                
                _loc_4 = this.recObjList[_loc_3].rec.contains(event.stageX, event.stageY);
                if (_loc_4 && !this.recObjList[_loc_3].isIn)
                {
                    if (this.recObjList[_loc_3].overCallBack != null)
                    {
                        this.recObjList[_loc_3].overCallBack.call(null);
                    }
                    this.recObjList[_loc_3].isIn = true;
                }
                else if (!_loc_4 && this.recObjList[_loc_3].isIn)
                {
                    if (this.recObjList[_loc_3].outCallBack != null)
                    {
                        this.recObjList[_loc_3].outCallBack.call(null);
                    }
                    this.recObjList[_loc_3].isIn = false;
                }
                _loc_3 = _loc_3 - 1;
            }
            return;
        }// end function

        protected function onMouseClick(event:MouseEvent) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.StageMouseClick, event));
            return;
        }// end function

        public function addRecEvent(param1:Rectangle, param2:Function, param3:Function) : void
        {
            var _loc_4:* = new RecObj(param1, param2, param3);
            this.recObjList.push(_loc_4);
            return;
        }// end function

        public function removeRecEvent(param1:Rectangle) : void
        {
            var _loc_2:* = this.recObjList.length;
            var _loc_3:* = _loc_2 - 1;
            while (_loc_3 >= 0)
            {
                
                if (this.recObjList[_loc_3].rec == param1)
                {
                    this.recObjList.splice(_loc_3, 1);
                }
                _loc_3 = _loc_3 - 1;
            }
            return;
        }// end function

        public static function get instance() : StageMouseManager
        {
            if (!_instance)
            {
                _instance = new StageMouseManager;
            }
            return _instance;
        }// end function

    }
}

class RecObj extends Object
{
    public var rec:Rectangle;
    public var overCallBack:Function;
    public var outCallBack:Function;
    public var isIn:Boolean = false;

    function RecObj(param1:Rectangle, param2:Function, param3:Function)
    {
        this.rec = param1;
        this.overCallBack = param2;
        this.outCallBack = param3;
        return;
    }// end function

}

